// pages/shiping/shiping.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      videoGroupList:[],
      navId:0,
      videoListData:[],
      videoId:'',
      videoUpdateTime:[],
      videoTimeObj:'',
      isTriggered:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.getvideoGroupList()

  
  },
  changeNav(e){
    let navId = e.currentTarget.id
    console.log(navId);
    this.setData({
      navId:navId>>>0,
      videoListData:[]
    })
    wx.showLoading({
      title: '正在加载',
    })
    this.getVideoListData(this.data.navId)
   
  },
  hanleUpdate(e){
  //  将视频id和视频播放时长获取
    let videoTimeObj = {vid:e.currentTarget.id,currentTime:e.detail.currentTime}
  // 
   let {videoUpdateTime} = this.data
    let videoItem = videoUpdateTime.find(item => item.vid === videoTimeObj.vid)
    if(videoItem){
      videoItem.currentTime = e.detail.currentTime
    }else {
      videoUpdateTime.push(videoTimeObj)
    }
   
 


   
  },
  handlePlay(e){
    let vid = e.currentTarget.id
    // 关闭上一个视频
    this.vid !== vid && this.videoContext && this.videoContext.stop()
    this.vid  = vid 
    this.setData({
      videoId:vid
    })
     // 创建控制video标签的实例对象
    this.videoContext = wx.createVideoContext(vid)
    let {videoUpdateTime} =this.data
    let videoItem = videoUpdateTime.find(item => item.vid === vid)
    if(videoItem) {
      this.videoContext.seek(videoItem.currentTime)
    }
    
    
  
  
  },
  handleEnded(e){
    let {videoUpdateTime} = this.data
    
    videoUpdateTime.splice(videoUpdateTime.findIndex(item => item.id === e.currentTarget.id),1)
    this.setData({
      videoUpdateTime
    })
  },
  handleRefresher(){
  
  this.getVideoListData(this.data.navId)

  },
  handleTolower(){
    console.log(666);
  },
 async getVideoList(navId){
    let res =await request('/video/group',{id:navId})
    
  },
 async getvideoGroupList(){
    // videoGroupList
    let  res =  await request('/video/group/list')
   let videoGroupList =  res.data.slice(0,14)
   let navId = res.data[0].id
   
    this.setData({
      videoGroupList,
      navId    
    })
  
    this.getVideoList(this.data.navId)
    this.getVideoListData(this.data.navId)
  },
  async getVideoListData(navId){
    let res = await request('/video/group',{id:navId})
    wx.hideLoading()
    
    let index = 0
    let videoListData =res.datas.map(item => {
      item.id = index++;
      return item
    })
    this.setData({
      videoListData,
      isTriggered:false
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function ({from}) {
     console.log(from);
     if(from === 'button') {
      return {
        title:'视频下角转发',
        page:'/pages/video/video',
        imageUrl:'/static/images/nvsheng.jpg'
      }
     }else {
      return {
        title:'主页菜单上转发',
        page:'/pages/video/video',
        imageUrl:'/static/images/nvsheng.jpg'
      }
     }
    

  }
})